const express = require('express');
const Mock = require('mockjs');
const router = express.Router();

// 获取用户列表
router.get('/', (req, res) => {
  const { page = 1, pageSize = 10 } = req.query;
  
  const listTemplate = `list|${pageSize}`;
  
  const data = Mock.mock({
    [listTemplate]: [
      {
        'id|+1': (parseInt(page) - 1) * parseInt(pageSize) + 1,
        'name': '@cname',
        'age|18-60': 1,
        'gender|1': ['男', '女'],
        'email': '@email',
        'phone': /^1[3456789]\d{9}$/,
        'address': '@county(true)',
        'createTime': '@datetime'
      }
    ],
    page: parseInt(page),
    pageSize: parseInt(pageSize),
    'total|100-200': 1
  });
  
  res.json(data);
});

// 获取单个用户
router.get('/:id', (req, res) => {
  const { id } = req.params;
  
  const data = Mock.mock({
    'id': id,
    'name': '@cname',
    'age|18-60': 1,
    'gender|1': ['男', '女'],
    'email': '@email',
    'phone': /^1[3456789]\d{9}$/,
    'address': '@county(true)',
    'createTime': '@datetime',
    'profile': '@paragraph'
  });
  
  res.json(data);
});

// 创建用户
router.post('/', (req, res) => {
  res.json({
    code: 0,
    message: '创建用户成功',
    data: req.body
  });
});

// 更新用户
router.put('/:id', (req, res) => {
  res.json({
    code: 0,
    message: '更新用户成功',
    data: req.body
  });
});

// 删除用户
router.delete('/:id', (req, res) => {
  res.json({
    code: 0,
    message: '删除用户成功'
  });
});

module.exports = router;